//
//  VisitorLoginView.swift
//  SinaWeiBo
//
//  Created by yz on 16/1/31.
//  Copyright © 2016年 yz. All rights reserved.
//

import UIKit


protocol VisitorLoginViewDelegate:NSObjectProtocol{
    
    //协议方法
    //登录方法
    func visitorWilllLogin()
    
     //注册方法
    func visitorWillRegister()
    
}


class VisitorLoginView: UIView {
    
     //申明代理属性
    weak var visitorDelegate:VisitorLoginViewDelegate?
    
    @objc private func loginDidClick()
    {
        visitorDelegate?.visitorWilllLogin()
        
    }
    
    @objc private func registerDidClick()
    {
        visitorDelegate?.visitorWillRegister()

    }
    
    
    
    
    func setBgUiInfo(Picname:String?,info:String)
    {

        iconView.hidden = false

          if(Picname != nil)
         {
            
            circleView.image = UIImage(named:Picname!)
            bringSubviewToFront(circleView)
            iconView.hidden = true
            infoLabel.text = info

         }else
          {
            startAnimation()
        }
    }

    
    //动画
    private func startAnimation(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 10
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    //重写构造方法
    init()
   {
    
    super.init(frame: CGRectZero)
    
    setupUI()
    

    
    }

    required init?(coder aDecoder: NSCoder) {

     super.init(coder: aDecoder)
     
     setupUI()
        
    }
    
   // 设置访客视图
   private func setupUI() {
    
    backgroundColor = UIColor(white: 0.93, alpha: 1)
    
    addSubview(circleView)
    addSubview(masksView)

    addSubview(iconView)

    addSubview(infoLabel)
    
    addSubview(Login)
    addSubview(Register)
    
    
    //设置frame布局失效
    for v in subviews{
        
        v.translatesAutoresizingMaskIntoConstraints = false
        
    }
    
    addConstraint( NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
    
    addConstraint( NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -50))

    addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
    
    addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))

    
    //infolabel设置
    addConstraint( NSLayoutConstraint(item: infoLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
    addConstraint( NSLayoutConstraint(item: infoLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 26))
    
   //infolabel宽度约束
    addConstraint(NSLayoutConstraint(item: infoLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
    addConstraint(NSLayoutConstraint(item: infoLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))

    
    
    //login按钮约束
    addConstraint(NSLayoutConstraint(item: Login, attribute: .Left, relatedBy: .Equal, toItem: infoLabel, attribute: .Left, multiplier: 1.0, constant: 0.0))
    addConstraint(NSLayoutConstraint(item:Login, attribute: .Top, relatedBy: .Equal, toItem: infoLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
    addConstraint(NSLayoutConstraint(item: Login, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
    addConstraint(NSLayoutConstraint(item: Login, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
    
    //register按钮约束
    addConstraint(NSLayoutConstraint(item: Register, attribute: .Right, relatedBy: .Equal, toItem: infoLabel, attribute: .Right, multiplier: 1.0, constant: 0.0))
    addConstraint(NSLayoutConstraint(item:Register, attribute: .Top, relatedBy: .Equal, toItem: infoLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
    addConstraint(NSLayoutConstraint(item: Register, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
    addConstraint(NSLayoutConstraint(item: Register, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
    
    //设置背景视图的约束
    
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[masksView]-0-|", options: [], metrics: nil, views: ["masksView":masksView]))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[masksView]-(-35)-[Register]", options: [], metrics: nil, views: ["masksView":masksView,"Register":Register]))
    
    
    Login.addTarget(self, action: "loginDidClick", forControlEvents: UIControlEvents.TouchUpInside)
    
    Register.addTarget(self, action: "registerDidClick", forControlEvents: UIControlEvents.TouchUpInside)


    
    }
    
    
    //懒加载所有控件
    
    private lazy var circleView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var masksView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

    
    
    private lazy var infoLabel : UILabel = {
    
        let info = UILabel()
        
        info.text = "关注一些人,回这里看看有什么惊喜关注一些人,回这里看看有什么惊喜"
        
        info.textAlignment = NSTextAlignment.Center
        
        info.font = UIFont.systemFontOfSize(14)
        
        info.textColor = UIColor.lightGrayColor()
        
        info.numberOfLines = 0
        
        
        
        info.sizeToFit()
        
        return info
    
    }()
    
    
    private lazy var Login : UIButton = {
       
        let loginBtn = UIButton ()
        
        loginBtn.setTitle("登录", forState: .Normal)
        
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        loginBtn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        
        return loginBtn
        
    }()
    
    private lazy var Register : UIButton = {
        
        let registerBtn = UIButton ()
        
        registerBtn.setTitle("注册", forState: .Normal)
        
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        return registerBtn
        
    }()
}
